package org.rufolfheszele.andpixelbyandpixel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.rufolfheszele.andpixelbyandpixel.configuration.Configuration;

import android.app.Application;
import android.content.Context;

/**
 * Classes to be ported					Ported
 * App									Yes (Half)
 * ClientStartingGameStateController	No
 * Configuration						Yes (Half)
 * DynamicDrawController				No
 * GameFinishedGameStateController		No
 * GameStartedGameStateController		No
 * GameStartedSubStateController		No
 * IController							Yes
 * IdleGameStateController				Yes
 * IDrawController						Yes
 * IGameController						Yes
 * IGameStateController					Yes
 * LevelDownloadingGameStateController	No
 * LevelUploadingGameStateController	No
 * LocalTurnSubStateController			No
 * LogoDrawController					Yes
 * MainFrame							Yes (MainActivity)
 * MainGameController					Yes (Half)
 * MainPanel							Yes (MainImageViewer)
 * MessagingFrame						No
 * MessagingPanel						No
 * NetworkEventData						Yes
 * RemoteTurnSubStateController			No
 * ServerRegistrationHandler			No
 * ServerStartingGameStateController	No
 * SmartPointer							No (Not needed)
 * StaticDrawController					Yes
 * Vehicle								Yes
 * WxUtils								Yes (Half - Utils)
 */

/**
 * 
 * @author erudhes
 *
 */

public class MainApplication extends Application
{
	public static MainApplication getMainApplication()
    {
    	return sMainApplication;
    }
	
	/**
     * This method is invoked when the application launches
     */
    @Override
    public void onCreate()
    {
            super.onCreate();
            sMainApplication = this;
    }
    
    public void onStop()
    {
    	// Make sure to save every possible changes
    	mConfiguration.write();
    }
    
    public void registerMainActivity(MainActivity mainActivity)
    {
    	mMainActivity = mainActivity;
    	// Check if we need to copy the configuration files from the assets directory to the internal storage
    	// This is only needed at the first launch of the application
    	// TODO: Copy everything, and assume a fix directory hierarchy in under the assets directory
    	if(0 == mMainActivity.fileList().length)
    	{
    		// There are no private files yet, so copy every configuration file
    		try
    		{
				InputStream input = MainApplication.getMainApplication().getMainActivity().getResources().getAssets().open("config.xml");
				// TODO: How to solve the unknown path at this time?
				String path = "res/levels";
				String[] levels = MainApplication.getMainApplication().getMainActivity().getAssets().list(path);
				FileOutputStream output = mMainActivity.openFileOutput("config.xml", Context.MODE_PRIVATE);
				byte[] data = new byte[input.available()];
				
				// Copy the main config.xml file
				input.read(data);
				output.write(data);
				input.close();
				output.close();
				// Create the directory for the levels
				mMainActivity.getDir("res", Context.MODE_PRIVATE);
				mMainActivity.getDir(path, Context.MODE_PRIVATE);
				// Copy the levels' config.xml files
				for(int i = 0; i < levels.length; ++i)
				{
					try
					{
						input = MainApplication.getMainApplication().getMainActivity().getResources().getAssets().open(path + "/" + levels[i] + "/config.xml");
						// First make the directory
						mMainActivity.getDir(path + "/" + levels[i], Context.MODE_PRIVATE);
						output = mMainActivity.openFileOutput(path + "/" + levels[i] + "/config.xml", Context.MODE_PRIVATE);
						data = new byte[input.available()];
						
						// Copy the config.xml file
						input.read(data);
						output.write(data);
						input.close();
						output.close();
					}
					catch (IOException exception)
		    		{
						// If we are here, the file most probably not exists, so just continue
						continue;
					}
				}
			}
    		catch (IOException exception)
    		{
				// TODO Auto-generated catch block
			}
    	}
    	mConfiguration = new Configuration();
    }
    
    public MainActivity getMainActivity()
    {
    	return mMainActivity;
    }
    
    public Configuration getConfiguration()
    {
    	return mConfiguration;
    }
    
    public Socket getClientSocket()
    {
    	return mClientSocket;
    }
    
    public void setClientSocket(Socket clientSocket)
    {
    	mClientSocket = clientSocket;
    }
    
    public ServerSocket getServerSocket()
    {
    	return mServerSocket;
    }
    
	public void setServerSocket(ServerSocket serverSocket)
	{
		mServerSocket = serverSocket;
	}
	
	public String getActualLevel()
	{
		return mActualLevel;
	}
	
	public void setActualLevel(String actualLevel)
	{
		mActualLevel = actualLevel;
	}
	
	public void resetActualLevel()
	{
		mActualLevel = "";
	}
	
	private boolean copyAssets()
	{
		boolean returnValue = false;
		
		try
		{
			String[] root = MainApplication.getMainApplication().getMainActivity().getAssets().list("");
			
			//TODO: How the hell can I make difference between directory and a simple file???
			
			
			
			
			InputStream input = MainApplication.getMainApplication().getMainActivity().getResources().getAssets().open("config.xml");
			// TODO: How to solve the unknown path at this time?
			String path = "res/levels";
			String[] levels = MainApplication.getMainApplication().getMainActivity().getAssets().list(path);
			FileOutputStream output = mMainActivity.openFileOutput("config.xml", Context.MODE_PRIVATE);
			byte[] data = new byte[input.available()];
			
			// Copy the main config.xml file
			input.read(data);
			output.write(data);
			input.close();
			output.close();
			// Create the directory for the levels
			mMainActivity.getDir("res", Context.MODE_PRIVATE);
			mMainActivity.getDir(path, Context.MODE_PRIVATE);
			// Copy the levels' config.xml files
			for(int i = 0; i < levels.length; ++i)
			{
				try
				{
					input = MainApplication.getMainApplication().getMainActivity().getResources().getAssets().open(path + "/" + levels[i] + "/config.xml");
					// First make the directory
					mMainActivity.getDir(path + "/" + levels[i], Context.MODE_PRIVATE);
					output = mMainActivity.openFileOutput(path + "/" + levels[i] + "/config.xml", Context.MODE_PRIVATE);
					data = new byte[input.available()];
					
					// Copy the config.xml file
					input.read(data);
					output.write(data);
					input.close();
					output.close();
				}
				catch (IOException exception)
	    		{
					// If we are here, the file most probably not exists, so just continue
					continue;
				}
			}
		}
		catch (IOException exception)
		{
			// TODO Auto-generated catch block
		}
		
		return returnValue;
	}
    
    private static MainApplication sMainApplication	= null;
    private MainActivity mMainActivity				= null;
    private Socket mClientSocket					= null;
    private ServerSocket mServerSocket				= null;
    private String mActualLevel						= null;
    private Configuration mConfiguration			= null;
}
